<?php
namespace app\admin\controller;

use think\Db;
use think\Session;
use think\input;
use app\admin\model\User;
use app\admin\controller\Base;

class Login extends Base
{
    public function index()
    {
        $this->ifLogin();
        return view();
    }


    public function checkLogin($username='',$password='',$verify=''){
        $captcha = input('verify');
        if (!captcha_check($captcha)) {
            $this->error('验证码错误','login/index');
        }

        $user = User::get([
            'username' => $username,
            'password' => md5($password)
        ]);

        if($user){
            Session::set('name',$username,'expire',3600);
//            Session::set('session_start_time', time());
            $db = Db::name('set');
            $res = $db->select();
            $id = $res[0]['switch'];
            if ($id == 1){
                $this->success('已经登录，正在跳转到设置活动数字页面', url('admin/index/activity'));
            }elseif ($id == 0){
                $this->success('登录成功', url('admin/index/index'));
            }
        }else{
            return $this->error('登录失败,账户或者密码错误');
        }
    }

    public function loginout(){
        Session::delete('name');
        $db1 = Db::name('set');
        $db2 = Db::name('admin_chars');
        $db1->where('id',1)->update(['switch'=>0]);
        for ($i = 0;$i<25;$i++){
            $db2->where('id',$i+1)->update(['stat'=>1]);
        }

        Db::query($sql = 'TRUNCATE table `bx_member_middle`');
        $this->success('关闭活动成功，正在退出','login/index');

    }

}
